<template>
  <div class="breadcrumb-style">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/ums/admin' }">权限</el-breadcrumb-item>
      <el-breadcrumb-item>分配资源</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <div class="custom-tree-node-container box">
    <el-tree
      :data="data"
      show-checkbox
      node-key="id"
      default-expand-all
      :expand-on-click-node="false"
      :props="{ class: customNodeClass }"
    />
    <div class="button">
      <el-button>清空</el-button>
      <el-button type="primary">保存</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import type Node from "element-plus/es/components/tree/src/model/node";
import { resource } from "../../request/api";
import { ref } from "vue";

let msg = ref(<any>[]);
resource.AllocResource().then((res) => {
  // console.log(res);
  let id = 10;
  let child = []; //商品模块
  let child2 = []; //订单模块
  let child3 = []; //营销模块
  let child4 = []; //权限模块
  let child5 = []; //内容模块
  let child6 = []; //其他模块
  function A(e, i, child) {
    //处理数据的函数
    if (res[i].categoryId == e) {
      child.push({
        id: res[i].id,
        label: res[i].name,
      });
    }
  }

  for (let i = 0; i < res.length; i++) {
    A(1, i, child);
    A(2, i, child2);
    A(3, i, child3);
    A(4, i, child4);
    A(5, i, child5);
    A(6, i, child6);
  }
  let z = [
    {
      id: id++,
      label: "商品模块",
      isPenultimate: true,
      children: child,
    },
    {
      id: id++,
      label: "订单模块",
      isPenultimate: true,
      children: child2,
    },
    {
      id: id++,
      label: "营销模块",
      isPenultimate: true,
      children: child3,
    },
    {
      id: id++,
      label: "权限模块",
      isPenultimate: true,
      children: child4,
    },
    {
      id: id++,
      label: "内容模块",
      isPenultimate: true,
      children: child5,
    },
    {
      id: id++,
      label: "其他模块",
      isPenultimate: true,
      children: child6,
    },
  ];
  msg.value = z;
  // console.log(msg.value);
});
interface Tree {
  id: number;
  label: string;
  isPenultimate?: boolean;
  children?: Tree[];
}

const customNodeClass = (data: Tree, node: Node) => {
  if (data.isPenultimate) {
    return "is-penultimate";
  }
  return null;
};

const data: Tree[] = msg;
</script>

<style>
.breadcrumb-style {
  width: 100%;
  height: 30px;
  background-color: #fff;
  border-bottom: 1px solid #eee;
}
.el-tree-node.is-expanded.is-penultimate > .el-tree-node__children {
  display: flex;
  flex-direction: row;
}
.is-penultimate > .el-tree-node__children > div {
  width: 25%;
}
.box {
  width: 95%;
  height: 100%;
  padding: 20px;
  margin: 20px 0;
  flex-wrap: wrap;
  border: 1px solid #ebeef5;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.3);
}
.button {
  margin-top: 20px;
  text-align: center;
}
.el-tree {
  padding: 20px;
  /* white-space: pre-wrap */
  height: 400px;
  line-height: 400px;
}
</style>
